package cn.duhengkui.national.poi.saxexcel;

import java.io.IOException;
import java.io.Writer;
import java.util.Calendar;

import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.ss.usermodel.DateUtil;

import cn.duhengkui.national.poi.utils.XMLEncoder;

public class SpreadsheetWriter {
	private final Writer _out;
	private int _rownum;
	private static String LINE_SEPARATOR = System.getProperty("line.separator");

	public SpreadsheetWriter(Writer out) {
		_out = out;
	}
	
	public void genXmlString() throws IOException{
		_out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
	}
	
	public void startWorkSheet() throws IOException{
		_out.write("<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">");
	}
	
	public void genSheetFormatPr() throws IOException{
		_out.write("<sheetFormatPr defaultRowHeight=\"20.0\"/>");
	}

	public void genCols(int[] width) throws IOException {

		_out.write("<cols>");
		if(width != null){
			for(int i = 0; i < width.length;i++){
				_out.write("<col min=\""+ (i+1) +"\" max=\""+ (i+1) +"\" width=\""+ width[i] +"\" customWidth=\"true\"/>");
			}
		}
		_out.write("</cols>");
		
	}
	
	public void startSheetData() throws IOException {
		_out.write("<sheetData>"+LINE_SEPARATOR);
	}

	public void endSheetData() throws IOException {
		_out.write("</sheetData>");

	}
	
	public void pageMargins() throws IOException{
		_out.write("<pageMargins bottom=\"0.75\" footer=\"0.3\" header=\"0.3\" left=\"0.7\" right=\"0.7\" top=\"0.75\"/>");
	}
	
	public void endWorksheet() throws IOException{
		_out.write("</worksheet>");
	}

	/**
	 * 写入行头
	 *
	 * @param rownum 行数
	 */
	public void insertRow(int rownum) throws IOException {
		_out.write("<row r=\"" + (rownum + 1) + "\">"+LINE_SEPARATOR);
		this._rownum = rownum;
	}

	/**
	 * 写入行尾
	 */
	public void endRow() throws IOException {
		_out.write("</row>"+LINE_SEPARATOR);
	}

	/**
	 * 创建格
	 * @param columnIndex
	 * @param value
	 * @param styleIndex
	 * @throws IOException
	 */
	public void createCell(int columnIndex, String value, int styleIndex)
			throws IOException {
		String ref = new CellReference(_rownum, columnIndex)
				.formatAsString();
		_out.write("<c r=\"" + ref + "\" t=\"inlineStr\"");
		if (styleIndex != -1)
			_out.write(" s=\"" + styleIndex + "\"");
		_out.write(">");
		_out.write("<is><t>"+XMLEncoder.encode(value)+"</t></is>");
		_out.write("</c>");
	}

	public void createCell(int columnIndex, String value)
			throws IOException {
		createCell(columnIndex, value, -1);
	}

	public void createCell(int columnIndex, double value, int styleIndex)
			throws IOException {
		String ref = new CellReference(_rownum, columnIndex)
				.formatAsString();
		_out.write("<c r=\"" + ref + "\" t=\"n\"");
		if (styleIndex != -1)
			_out.write(" s=\"" + styleIndex + "\"");
		_out.write(">");
		_out.write("<v>" + value + "</v>");
		_out.write("</c>");
	}

	public void createCell(int columnIndex, double value)
			throws IOException {
		createCell(columnIndex, value, -1);
	}

	public void createCell(int columnIndex, Calendar value, int styleIndex)
			throws IOException {
		createCell(columnIndex, DateUtil.getExcelDate(value, false),
				styleIndex);
	}
	
	public void write(String xmlString) throws IOException{
		_out.write(xmlString);
	}
	
}
